import {
  queryPageDataReq,
  addItemReq,
  queryStaffReq,
  updateItemReq,
  deleteItemReq,
  toggleStateReq,
  queryItemInfoReq,
  sendSmsReq,
} from "./services";
import { namespace } from "./configFile";
import { message } from "antd";
import { queryVenueReq } from "@/pages/stadium/classroom/services";

export default {
  namespace,
  state: {
    init: false,
    data: {},
  },
  effects: {
    *pageInit({ params, callback }, { call, put }) {
      const staff = yield call(queryStaffReq, { userId: params.userId });
      const res = yield call(queryPageDataReq, params);
      const venues = yield call(queryVenueReq, {});
      if (res) {
        yield put({
          type: "save",
          payload: {
            ...params,
            ...res,
          },
        });

        callback && callback(res, staff, venues.records);
      }
    },
    *queryTable({ params, callback }, { call, put }) {
      const res = yield call(queryPageDataReq, params);
      if (res) {
        callback && callback(res);
      }
    },

    *submitItem({ params, callback }, { call }) {
      let res = yield call(addItemReq, params);
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *deleteItem({ params, callback }, { call }) {
      let res = yield call(deleteItemReq, params);
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *queryItemInfo({ params, callback }, { call }) {
      let res = yield call(queryItemInfoReq, params);
      if (res) {
        callback && callback(res);
      }
    },

    *toggleState({ params, callback }, { call }) {
      let res = yield call(toggleStateReq, params);
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *sendSms({ params, callback }, { call }) {
      let res = yield call(sendSmsReq, params);
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback(res.data);
      } else {
        message.error(res.msg);
      }
    },
  },
  reducers: {
    save(state: any, { payload }: any) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
